<HTML>
<HEAD>
<TITLE>
AutumnConfig.cpp
</TITLE>
</HEAD>
<BODY>
<PRE>
<font color="green">/*
* Copyright 2006 the original author or authors.
* 
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* 
*      http://www.apache.org/licenses/LICENSE-2.0
* 
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/</font>

<font color="blue">#include</font> <font color="maroon">&#60;memory&#62;</font>
<font color="blue">#include</font> <font color="maroon">"AutumnDefinition.h"</font>
<font color="blue">#include</font> <font color="maroon">"AutumnException.h"</font>
<font color="blue">#include</font> <font color="maroon">"IBeanWrapper.h"</font>
<font color="blue">#include</font> <font color="maroon">"ILibrary.h"</font>
<font color="blue">#include</font> <font color="maroon">"LocalLibrary.h"</font>
<font color="blue">#include</font> <font color="maroon">"DynamicLibrary.h"</font>
<font color="blue">#include</font> <font color="maroon">"AutumnConfig.h"</font>

<font color="blue">namespace</font> Autumn<font color="black">{</font>

AutumnConfig<font color="black">:</font><font color="black">:</font>AutumnConfig<font color="black">(</font>IResource<font color="black">*</font> config<font color="black">)</font>
<font color="black">{</font>
    <font color="blue">const</font> vector<font color="black">&#60;</font>TLibrary<font color="black">*</font><font color="black">&#62;</font> <font color="black">*</font>pLibs <font color="black">=</font> config<font color="black">-</font><font color="black">&#62;</font>getLibraries<font color="black">(</font><font color="black">)</font>;
    <font color="blue">for</font><font color="black">(</font><font color="blue">int</font> i<font color="black">=</font><font color="maroon">0</font>; i<font color="black">&#60;</font>pLibs<font color="black">-</font><font color="black">&#62;</font>size<font color="black">(</font><font color="black">)</font>; i<font color="black">+</font><font color="black">+</font><font color="black">)</font><font color="black">{</font>
        <font color="blue">this</font><font color="black">-</font><font color="black">&#62;</font>processLibrary<font color="black">(</font><font color="black">*</font><font color="black">(</font><font color="black">*</font>pLibs<font color="black">)</font><font color="black">[</font>i<font color="black">]</font><font color="black">)</font>;
    <font color="black">}</font>

<font color="black">}</font>

<font color="blue">void</font> AutumnConfig<font color="black">:</font><font color="black">:</font>processLibrary<font color="black">(</font>TLibrary<font color="black">&</font> lib<font color="black">)</font>
<font color="black">{</font>
    ILibrary<font color="black">*</font> pl;

    <font color="blue">if</font><font color="black">(</font> lib.Path <font color="black">!</font><font color="black">=</font> LocalLibraryPath<font color="black">)</font><font color="black">{</font>
        pl <font color="black">=</font> <font color="blue">new</font> DynamicLibrary<font color="black">(</font>lib.Name, lib.Path<font color="black">)</font>;
        <font color="blue">this</font><font color="black">-</font><font color="black">&#62;</font>Libs.push_back<font color="black">(</font>pl<font color="black">)</font>;
    <font color="black">}</font>
    <font color="blue">else</font><font color="black">{</font>
        pl <font color="black">=</font> LocalLibrary<font color="black">:</font><font color="black">:</font>getInstance<font color="black">(</font><font color="black">)</font>;
    <font color="black">}</font>
    
    <font color="blue">int</font> i;
    <font color="blue">for</font><font color="black">(</font>i<font color="black">=</font><font color="maroon">0</font>; i<font color="black">&#60;</font>lib.Beans.size<font color="black">(</font><font color="black">)</font>; i<font color="black">+</font><font color="black">+</font><font color="black">)</font><font color="black">{</font>
        <font color="blue">this</font><font color="black">-</font><font color="black">&#62;</font>processBean<font color="black">(</font>lib.Beans<font color="black">[</font>i<font color="black">]</font>, pl<font color="black">)</font>;
    <font color="black">}</font>

    <font color="blue">for</font><font color="black">(</font>i<font color="black">=</font><font color="maroon">0</font>; i<font color="black">&#60;</font>lib.TypeNames.size<font color="black">(</font><font color="black">)</font>; i<font color="black">+</font><font color="black">+</font><font color="black">)</font><font color="black">{</font>
        <font color="blue">this</font><font color="black">-</font><font color="black">&#62;</font>TypeNames.push_back<font color="black">(</font>lib.TypeNames<font color="black">[</font>i<font color="black">]</font><font color="black">)</font>;
    <font color="black">}</font>
<font color="black">}</font>

<font color="green">/** Deal with each bean */</font>
<font color="blue">void</font> AutumnConfig<font color="black">:</font><font color="black">:</font>processBean<font color="black">(</font>TBean<font color="black">&</font> bean, ILibrary<font color="black">*</font> pl<font color="black">)</font>
<font color="black">{</font>
    <font color="green">// the bean exists or not</font>
    <font color="blue">if</font><font color="black">(</font> <font color="blue">this</font><font color="black">-</font><font color="black">&#62;</font>Beans.find<font color="black">(</font>bean.Name<font color="black">)</font> <font color="black">!</font><font color="black">=</font> <font color="blue">this</font><font color="black">-</font><font color="black">&#62;</font>Beans.end<font color="black">(</font><font color="black">)</font> <font color="black">)</font><font color="black">{</font>
        <font color="blue">throw</font> ReduplicateEx<font color="black">(</font><font color="maroon">"AutumnConfig"</font>, <font color="maroon">"processBean"</font>, 
            <font color="maroon">"Reduplicate defined bean ["</font> <font color="black">+</font> bean.Name <font color="black">+</font> <font color="maroon">"]!"</font><font color="black">)</font>;
    <font color="black">}</font>

    <font color="green">// get bean wrapper maker function</font>
    string funName <font color="black">=</font> <font color="blue">this</font><font color="black">-</font><font color="black">&#62;</font>mangleName<font color="black">(</font><font color="maroon">"bean"</font>, <font color="maroon">"create"</font>, bean.ClassName<font color="black">)</font>;
    WrapperMaker<font color="black">*</font> pw <font color="black">=</font> <font color="black">(</font>WrapperMaker<font color="black">*</font><font color="black">)</font>pl<font color="black">-</font><font color="black">&#62;</font>getFunction<font color="black">(</font>funName<font color="black">)</font>;
    <font color="blue">if</font><font color="black">(</font> pw <font color="black">=</font><font color="black">=</font> NULL <font color="black">)</font><font color="black">{</font>
        <font color="blue">throw</font> NotFoundEx<font color="black">(</font><font color="maroon">"AutumnConfig"</font>, <font color="maroon">"processBean"</font>, 
            <font color="maroon">"Geting WrapperMaker failed for class '"</font> <font color="black">+</font> bean.ClassName <font color="black">+</font> <font color="maroon">"'."</font><font color="black">)</font>;
    <font color="black">}</font>

    <font color="green">// get bean wrapper freer function</font>
    funName <font color="black">=</font> <font color="blue">this</font><font color="black">-</font><font color="black">&#62;</font>mangleName<font color="black">(</font><font color="maroon">"bean"</font>, <font color="maroon">"delete"</font>, bean.ClassName<font color="black">)</font>;
    WrapperFreer<font color="black">*</font> pd <font color="black">=</font> <font color="black">(</font>WrapperFreer<font color="black">*</font><font color="black">)</font>pl<font color="black">-</font><font color="black">&#62;</font>getFunction<font color="black">(</font>funName<font color="black">)</font>;
    <font color="blue">if</font><font color="black">(</font> pd <font color="black">=</font><font color="black">=</font> NULL <font color="black">)</font><font color="black">{</font>
        <font color="blue">throw</font> NotFoundEx<font color="black">(</font><font color="maroon">"AutumnConfig"</font>, <font color="maroon">"processBean"</font>, 
            <font color="maroon">"Geting WrapperFreer failed for class '"</font> <font color="black">+</font> bean.ClassName <font color="black">+</font> <font color="maroon">"'."</font><font color="black">)</font>;
    <font color="black">}</font>
    
    <font color="green">// create BeanConfig</font>
    auto_ptr<font color="black">&#60;</font>BeanConfig<font color="black">&#62;</font> pb<font color="black">(</font><font color="blue">new</font> BeanConfig<font color="black">(</font>bean, pw, pd<font color="black">)</font><font color="black">)</font>;
    <font color="blue">this</font><font color="black">-</font><font color="black">&#62;</font>Beans.insert<font color="black">(</font>make_pair<font color="black">(</font>bean.Name, pb.release<font color="black">(</font><font color="black">)</font><font color="black">)</font><font color="black">)</font>;
<font color="black">}</font>

string AutumnConfig<font color="black">:</font><font color="black">:</font>mangleName<font color="black">(</font><font color="blue">const</font> string<font color="black">&</font> objType, 
                                <font color="blue">const</font> string<font color="black">&</font>  op, <font color="blue">const</font> string<font color="black">&</font> objName<font color="black">)</font>
<font color="black">{</font>
    <font color="blue">if</font><font color="black">(</font> objType <font color="black">=</font><font color="black">=</font> <font color="maroon">"bean"</font> <font color="black">)</font><font color="black">{</font>
        <font color="blue">if</font><font color="black">(</font> op <font color="black">=</font><font color="black">=</font> <font color="maroon">"create"</font> <font color="black">)</font>
            <font color="blue">return</font> <font color="maroon">"create_"</font> <font color="black">+</font> objName <font color="black">+</font> <font color="maroon">"_Wrapper"</font>;
        <font color="blue">else</font> <font color="blue">if</font><font color="black">(</font> op <font color="black">=</font><font color="black">=</font> <font color="maroon">"delete"</font> <font color="black">)</font>
            <font color="blue">return</font> <font color="maroon">"delete_"</font> <font color="black">+</font> objName <font color="black">+</font> <font color="maroon">"_Wrapper"</font>;
    <font color="black">}</font>
    <font color="blue">throw</font> NotFoundEx<font color="black">(</font><font color="maroon">"AutumnConfig"</font>, <font color="maroon">"mangleName"</font>, 
        <font color="maroon">"type["</font> <font color="black">+</font> objType <font color="black">+</font> <font color="maroon">"]-&#62;operation["</font> <font color="black">+</font> op <font color="black">+</font><font color="maroon">"] is not found!"</font><font color="black">)</font>;
<font color="black">}</font>

AutumnConfig<font color="black">:</font><font color="black">:</font>~AutumnConfig<font color="black">(</font><font color="black">)</font>
<font color="black">{</font>
    AutumnLog<font color="black">:</font><font color="black">:</font>getInstance<font color="black">(</font><font color="black">)</font><font color="black">-</font><font color="black">&#62;</font>debug<font color="black">(</font><font color="maroon">"AutumnConfig-&#62;~AutumnConfig"</font><font color="black">)</font>;
    <font color="blue">for</font><font color="black">(</font><font color="blue">int</font> i<font color="black">=</font><font color="maroon">0</font>; i<font color="black">&#60;</font><font color="blue">this</font><font color="black">-</font><font color="black">&#62;</font>Libs.size<font color="black">(</font><font color="black">)</font>; i<font color="black">+</font><font color="black">+</font><font color="black">)</font>
        <font color="blue">delete</font> <font color="blue">this</font><font color="black">-</font><font color="black">&#62;</font>Libs<font color="black">[</font>i<font color="black">]</font>;
    
    <font color="green">/*
     * Mustn't delete LocalLibrary singleton. BeanFactory maybe be created and deleted
     * many times, but LocalLibrary must be created at the time of the first local
     * function registering.
     */</font>
    <font color="green">//LocalLibrary::deleteInstance();</font>
    
    <font color="blue">for</font><font color="black">(</font>map<font color="black">&#60;</font>string, BeanConfig<font color="black">*</font><font color="black">&#62;</font><font color="black">:</font><font color="black">:</font>iterator it<font color="black">=</font><font color="blue">this</font><font color="black">-</font><font color="black">&#62;</font>Beans.begin<font color="black">(</font><font color="black">)</font>;
            it<font color="black">!</font><font color="black">=</font><font color="blue">this</font><font color="black">-</font><font color="black">&#62;</font>Beans.end<font color="black">(</font><font color="black">)</font>; it<font color="black">+</font><font color="black">+</font><font color="black">)</font>
        <font color="blue">delete</font> it<font color="black">-</font><font color="black">&#62;</font>second;
<font color="black">}</font>

BeanConfig<font color="black">*</font> AutumnConfig<font color="black">:</font><font color="black">:</font>getBeanConfig<font color="black">(</font><font color="blue">const</font> string<font color="black">&</font> <font color="blue">name</font><font color="black">)</font> <font color="blue">const</font>
<font color="black">{</font>
    <font color="blue">const</font> map<font color="black">&#60;</font>string, BeanConfig<font color="black">*</font><font color="black">&#62;</font><font color="black">:</font><font color="black">:</font>const_iterator it <font color="black">=</font> <font color="blue">this</font><font color="black">-</font><font color="black">&#62;</font>Beans.find<font color="black">(</font><font color="blue">name</font><font color="black">)</font>;
    <font color="blue">if</font><font color="black">(</font> <font color="blue">this</font><font color="black">-</font><font color="black">&#62;</font>Beans.end<font color="black">(</font><font color="black">)</font> <font color="black">!</font><font color="black">=</font> it <font color="black">)</font>
        <font color="blue">return</font> it<font color="black">-</font><font color="black">&#62;</font>second;
    <font color="blue">else</font>
        <font color="blue">return</font> NULL;
<font color="black">}</font>

<font color="black">}</font> <font color="green">// End namespace Autumn</font>

</PRE>
</BODY>
</HTML>
